-
-
Notifications
You must be signed in to change notification settings - Fork 771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature] Spy passes through calling with new
#1626
Conversation
This changes the spy behavior so that if the spy is `calledWithNew()` then the original function will also get called with `new`. This was throwing an error if you tried spying on an ES6 class object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this seems like a good thing. It doesn't break any existing tests (do fix the linting though!), and fixes a valid case for ES2015.
It needs a verification test that shows this is working though!
lib/sinon/spy.js
Outdated
} else { | ||
returnValue = (this.func || func).apply(thisValue, args); | ||
} | ||
|
||
if (thisCall.calledWithNew() && typeof returnValue !== "object") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps nest this within the first check?
I hope this is a decent enough way to unit test this behavior
@fatso83 Updated. Sorry for doing everything on the Github website vs a streamlined PR, didn't want to take time to clone/install/test/etc when it's all built into CI/CD |
Using a named function makes it far easier to follow what is happening in a debugger.
lib/sinon/spy.js
Outdated
if (thisCall.calledWithNew()) { | ||
// Call through with `new` | ||
// eslint-disable-next-line new-parens | ||
returnValue = new (Function.prototype.bind.apply(this.func || func, [thisValue].concat(args))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have a list of methods from the various built-in above, so I'll extract the bind
as a variable and move it up to be consistent and make it a bit more readable.
Thank you! I had a hard time understanding what was going on, so needed to play with the code and run it in a debugger and examine it before I could accept it. Made a tiny change, but your test was sufficient to prove everything still worked. Since the everything done here logically belonged in one commit it was ok that the history was a bit messy, as I could squash everything, including my commit. And yes, the unit test you added was fine :-) |
lib/sinon/spy.js
Outdated
@@ -195,7 +196,7 @@ var spyApi = { | |||
if (thisCall.calledWithNew()) { | |||
// Call through with `new` | |||
// eslint-disable-next-line new-parens |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line could probably have been removed with your addition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, true. thanks! Fixed in 6ba959e
Thank you! |
Was fixed by some minor changes in the squashed commit. Ref #1626 (comment)
This changes the spy behavior so that if the spy is `calledWithNew()` then the original function will also get called with `new`. This was throwing an error if you tried spying on an ES6 class object. fixes sinonjs#1265
Was fixed by some minor changes in the squashed commit. Ref sinonjs#1626 (comment)
Purpose (TL;DR)
This changes the spy behavior so that if the spy is
calledWithNew()
thenthe original function will also get called with
new
.This was throwing an error if you tried spying on an ES6 class object.
Fixes #1265
How to verify
Checklist for author
npm run lint
passes